package cn.com.blueInfo.bpm.person.repository.impl;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.repository.BaseRepository;
import cn.com.blueInfo.bpm.person.entity.GroupInfo;
import cn.com.blueInfo.bpm.person.enums.GroupInfoAsync;
import cn.com.blueInfo.bpm.person.enums.GroupInfoFormula;
import cn.com.blueInfo.bpm.person.enums.GroupInfoGroupType;
import cn.com.blueInfo.bpm.person.mapper.GroupInfoMapper;
import cn.com.blueInfo.bpm.person.repository.GroupInfoRepository;
import cn.com.blueInfo.bpm.person.request.GroupInfoDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 群组信息表 数据服务实现类
 * @author suxch
 * @since 2025-09-01 19:28:09
 */
@Log4j2
@Service
public class GroupInfoRepositoryImpl
        extends ServiceImpl<GroupInfoMapper, GroupInfo>
        implements GroupInfoRepository, BaseRepository<GroupInfo, GroupInfoDTO.QueryDTO> {

    @Autowired
    private CurrentLoginUserInfo currentLoginUserInfo;

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    @Override
    public CurrentLoginUserInfo getCurrentLoginUserInfo() {
        return currentLoginUserInfo;
    }

    /**
     * 新增群组信息
     * @param groupInfo 群组信息参数
     */
    @Override
    public Integer createGroupInfo(GroupInfo groupInfo) {
        return sqlExecuteResult(save(groupInfo));
    }

    /**
     * 批量新增群组信息
     * @param groupInfoList 群组信息参数列表
     */
    @Override
    public Integer batchCreateGroupInfo(List<GroupInfo> groupInfoList) {
        return sqlExecuteResult(saveBatch(groupInfoList), groupInfoList.size());
    }

    /**
     * 删除群组信息
     * @param groupInfoId 群组信息ID
     */
    @Override
    public Integer deleteGroupInfo(Long groupInfoId) {
        GroupInfo groupInfo = new GroupInfo();
        groupInfo.setId(groupInfoId);
        groupInfo.setDeleted(BaseDeleted.DELETED);
        return sqlExecuteResult(removeById(groupInfo));
    }

    /**
     * 批量删除群组信息
     * @param groupInfoIdList 群组信息ID列表
     */
    @Override
    public Integer batchDeleteGroupInfo(List<Long> groupInfoIdList) {
        List<GroupInfo> groupInfoList = new ArrayList<>();
        for (Long groupInfoId : groupInfoIdList) {
            GroupInfo groupInfo = new GroupInfo();
            groupInfo.setId(groupInfoId);
            groupInfo.setDeleted(BaseDeleted.DELETED);
            groupInfoList.add(groupInfo);
        }
        return sqlExecuteResult(removeBatchByIds(groupInfoList), groupInfoList.size());
    }

    /**
     * 更新部分群组信息
     * @param groupInfo 群组信息参数
     */
    @Override
    public Integer updateGroupInfoPartial(GroupInfo groupInfo) {
        validateUpdateId(groupInfo.getId());
        LambdaUpdateWrapper<GroupInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(GroupInfo::getId, groupInfo.getId());
        return sqlExecuteResult(update(updateWrapper));
    }

    /**
     * 更新全部群组信息
     * @param groupInfo 群组信息参数
     */
    @Override
    public Integer updateGroupInfo(GroupInfo groupInfo) {
        validateUpdateId(groupInfo.getId());
        return sqlExecuteResult(updateById(groupInfo));
    }

    /**
     * 查询一条群组信息
     * @param groupInfoId 群组信息ID
     */
    @Override
    public GroupInfo getGroupInfoById(Long groupInfoId) {
        LambdaQueryWrapper<GroupInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GroupInfo::getId, groupInfoId);
        return getOne(queryWrapper);
    }

    /**
     * 分页查询群组信息
     * @param groupInfoQueryDTO 群组信息参数
     */
    @Override
    public Page<GroupInfo> getGroupInfoPage(GroupInfoDTO.QueryDTO groupInfoQueryDTO) {
        Page<GroupInfo> groupInfoPage = new Page<>(
                groupInfoQueryDTO.getPage(), groupInfoQueryDTO.getSize());
        LambdaQueryWrapper<GroupInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, groupInfoQueryDTO);

        BaseDeleted deleted = groupInfoQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeletePage(groupInfoPage, queryWrapper);
        } else {
            return page(groupInfoPage, queryWrapper);
        }
    }

    /**
     * 根据条件查询 群组信息 数量
     * @param groupInfoQueryDTO 群组信息参数
     */
    @Override
    public Long getGroupInfoCount(GroupInfoDTO.QueryDTO groupInfoQueryDTO) {
        LambdaQueryWrapper<GroupInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, groupInfoQueryDTO);

        BaseDeleted deleted = groupInfoQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteCount(queryWrapper);
        } else {
            return count(queryWrapper);
        }
    }

    /**
     * 根据条件查询 群组信息
     * @param groupInfoQueryDTO 群组信息参数
     */
    @Override
    public List<GroupInfo> getGroupInfoList(GroupInfoDTO.QueryDTO groupInfoQueryDTO) {
        LambdaQueryWrapper<GroupInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, groupInfoQueryDTO);

        BaseDeleted deleted = groupInfoQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteList(queryWrapper);
        } else {
            return list(queryWrapper);
        }
    }

    private void queryWrapper(LambdaQueryWrapper<GroupInfo> queryWrapper, GroupInfoDTO.QueryDTO groupInfoQueryDTO) {

        String groupCode = groupInfoQueryDTO.getGroupCode();
        queryWrapper.eq(StringUtils.isNotBlank(groupCode), GroupInfo::getGroupCode, groupCode);

        String groupName = groupInfoQueryDTO.getGroupName();
        queryWrapper.eq(StringUtils.isNotBlank(groupName), GroupInfo::getGroupName, groupName);

        GroupInfoGroupType groupType = groupInfoQueryDTO.getGroupType();
        queryWrapper.eq(ObjectUtils.isNotEmpty(groupType), GroupInfo::getGroupType, groupType);

        String description = groupInfoQueryDTO.getDescription();
        queryWrapper.eq(StringUtils.isNotBlank(description), GroupInfo::getDescription, description);

        GroupInfoFormula formula = groupInfoQueryDTO.getFormula();
        queryWrapper.eq(ObjectUtils.isNotEmpty(formula), GroupInfo::getFormula, formula);

        GroupInfoAsync async = groupInfoQueryDTO.getAsync();
        queryWrapper.eq(ObjectUtils.isNotEmpty(async), GroupInfo::getAsync, async);

        addQueryWrapper(queryWrapper, groupInfoQueryDTO, getCommonColumn());

    }

    private Map<String, SFunction<GroupInfo, ?>> getCommonColumn() {
        return BaseEntity.getCommonColumn(
                GroupInfo::getOrderNumber,
                GroupInfo::getTenantId,
                GroupInfo::getApplicationId,
                GroupInfo::getStatus,
                GroupInfo::getDeleted,
                GroupInfo::getDeleteUserId,
                GroupInfo::getDeleteTime,
                GroupInfo::getRemark,
                GroupInfo::getCreateUserId,
                GroupInfo::getCreateTime,
                GroupInfo::getUpdateUserId,
                GroupInfo::getUpdateTime,
                GroupInfo::getExtend1,
                GroupInfo::getExtend2,
                GroupInfo::getExtend3);
    }

}
